const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    isLogin: false,
    authinfo: {},
    options: [
      // {id: 1, name: '我的钱包', icon: 'http://glack.oss-cn-shanghai.aliyuncs.com/qianbao.png'},
      {
        id: 2,
        name: '设置',
        icon: 'http://glack.oss-cn-shanghai.aliyuncs.com/shezhi.png'
      },
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let logininfo = wx.getStorageSync('logininfo')
    console.log(logininfo);

    if (logininfo.openid) {
      this.setData({
        authinfo: logininfo,
        isLogin: true
      })
      app.globalData.isLogin = true
      app.globalData.userinfo = logininfo
    }
  },

  onShow: function () {
    // console.log(app.globalData);

    // this.setData({
    //   isLogin: app.globalData.isLogin,
    //   authinfo: app.globalData.authinfo,
    // })
  },

  async gologin(e) {
    wx.getUserProfile({
      desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: async (result) => {
        let e = result

        console.log(result, 'result');
        
        
        if (e.errMsg !== "getUserProfile:ok") {
          return wx.showToast({
            title: '请授权~',
            icon: 'none'
          })
        }
        let authinfo = JSON.parse(e.rawData)
        app.setStorageAndGlobal(e)

        const res = await app._http({
          url: '/user/login',
          data: {
            ...authinfo,
            openid: app.globalData.openid
          },
          method: 'post'
        })

        if (res.data.code === 1) {
          let logininfo = res.data.data
          this.setData({
            authinfo: logininfo,
            isLogin: true
          })
          app.globalData.isLogin = true
          app.globalData.userinfo = logininfo
          wx.setStorageSync('logininfo', logininfo)
          wx.setStorageSync('isLogin', true)

        }

      }
    })




  },

  gotopath(e) {
    const {
      name
    } = e.currentTarget.dataset
    wx.showModal({
      title: '提示',
      content: `${name}暂未开发`,
      confirmColor: app.PCOLOR
    })
  }
})